﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using PMS.ENT;
using System.Data.SqlClient;
using PMS.Utilities;
using System.Configuration;
namespace PMS.DAL
{
    public class DashBoardDAL
    {
        #region Common
        private static DashBoardENT CreateCommentFromReader(IDataReader reader)
        {
            DashBoardENT item = new DashBoardENT();
            try
            {

                if (!reader.IsDBNull(reader.GetOrdinal(DashBoardColumns.AProject.ToString()))) item.AProject = Utility.IntParse(reader[DashBoardColumns.AProject.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(DashBoardColumns.BProject.ToString()))) item.BProject = Utility.IntParse(reader[DashBoardColumns.BProject.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(DashBoardColumns.AGroup.ToString()))) item.AGroup = Utility.IntParse(reader[DashBoardColumns.AGroup.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(DashBoardColumns.BGroup.ToString()))) item.BGroup = Utility.IntParse(reader[DashBoardColumns.BGroup.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(DashBoardColumns.ASchedule.ToString()))) item.ASchedule = Utility.IntParse(reader[DashBoardColumns.ASchedule.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(DashBoardColumns.AProgress.ToString()))) item.AProgress = Utility.IntParse(reader[DashBoardColumns.AProgress.ToString()]);

            }
            catch
            {
                throw;
            }
            return item;
        }
        #endregion

        #region Get
        public static DashBoardENT GetDashBoard(int userID,int roleID,int semesterID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand(Procedure.procDashboard.ToString());
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, userID);
                database.AddInParameter(dbCommand, "@SemesterID", DbType.Int32, semesterID);
                database.AddInParameter(dbCommand, "@RoleID", DbType.Int32, roleID);
                DashBoardENT dashboard = new DashBoardENT();

                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        dashboard = CreateCommentFromReader(reader);
                    }
                    reader.Close();
                }

                return dashboard;
            }
            catch (SqlException)
            {
                throw new DataAccessException("BLM_ERR_GetCommentByID");
            }
        }
        #endregion
    }
}
